

***********************************************************
* (1.0) Set up the survival analysis
***********************************************************
	
	use 	"$f_2PKbirthpanel", clear	

	keep if rural ==1
	gen marriage_year = .
	replace marriage_year = 1900 + year_marriage 
	
	gen marriage_age = age_marriage
	
	keep if pregresult<3
	keep if order_livebirth == 1 
	keep m_year_birth id prov LLFyear marriage_year educ marriage_age year prov
	rename year year_firstbirth 
	duplicates drop 

	**********************************************************
	* (1.1) First  output a dataset with 
	*		observations for each woman from birth to first birth 
	**********************************************************
	forval i = 1/49 { 
	preserve 	
		local  	age = `i' - 1 
		
		gen 	age = `age' 
		gen 	year = m_year_birth + `age' 
		
		keep if year >= 1964
		keep if year <= 1984
	
		gen 	eventyear = year - LLFyear
		
		gen 	mother = 0 
		replace mother = 1 if year_firstbirth<=year

		gen 	firstbirth = 0 
		replace firstbirth = 1 if year_firstbirth == year

		keep if year >= 1964
		keep if year <= 1984

		* Keep only women who are not yet mothers or have their first baby in the reference year
		drop if year_firstbirth < year
		
		if 		`i' == 1 { 
			tempfile age_firstbirth_panel
			save `age_firstbirth_panel', replace 
		} 
	
		else if `i' > 1 { 
			append 	using 	`age_firstbirth_panel'
			save `age_firstbirth_panel', replace 
		}
		
	restore 	
	} 
	

	**********************************************************
	* (1.2) Add control variables 
	**********************************************************

	use `age_firstbirth_panel', clear 

	cap drop _merge
	merge m:1 prov year using `"$d_data\ProductionData.dta"'
	
	cap drop _merge
	merge m:1 prov year using `"$d_data\IMR.dta"'

	gen 	LLF = 		LLFyear <= year
	replace LLF = . if 	LLFyear == . 	

	egen uniqueid = group(id)

	drop if age<15
	replace age = 40 if age > 40
	
	cap drop age_*
	tab age, gen(age_)
	
	cap drop LLF_age_*
	forval i = 1/26 { 
		gen LLF_age_`i' = LLF*age_`i'
		} 

	cap drop _merge
	merge m:1 prov year using `"$d_data\ProductionData.dta"'
	cap drop _merge
	merge m:1 prov year using `"$d_data\IMR.dta"'

	keep if eventyear >= -9 & eventyear <= 9 
	drop if year <1964
	drop if year >1979
	
	drop if year == 1967 
	drop if year == 1968 
	
	cap drop _merge
	merge m:1 prov using `"$d_data\sentdown_mean.dta"'
	drop _merge 
	
	gen 	culturalrev = 0 
	replace culturalrev = mean_sentdown if year >= 1966 & year <=1969
 
	do `"$d_do\LLF_StackData.do"'
		
		forval i = 1/9 {
		gen 	years_since_LLFpre`i' 	= 0 if eventyear != . 
		replace years_since_LLFpre`i' 	= 1 if eventyear == -`i'
		}

		gen 	years_since_LLFpost0 	= 0 if eventyear != . 
		replace years_since_LLFpost0 	= 1 if eventyear == 0 	

		forval i = 1/9 {
		gen 	years_since_LLFpost`i' 	= 0 if eventyear != . 
		replace years_since_LLFpost`i' 	= 1 if eventyear == `i'
		}


 	order year_firstbirth- culturalrev years_since_LLFpre9 years_since_LLFpre8 years_since_LLFpre7 ///
	years_since_LLFpre6 years_since_LLFpre5 years_since_LLFpre4 years_since_LLFpre3 years_since_LLFpre2 ///
	years_since_LLFpre1
	
	drop if eventyear>7
	drop if eventyear<-8
	char FE_prov[omit] 108 

**********************************************************
* (2.0) Main regressions
**********************************************************

	global controls1 	"i.educ  U5MR_5yr_ave"
	global controls2 	"grain agric_production gdp pop_total_agric"

	********************************
	* (2.1) Main Results
	********************************	
	tempfile main 
	qui save `main' , replace  
	
	xi: logit mother age_1-age_8 age_10-age_26 LLF LLF_age_1-LLF_age_8 LLF_age_10-LLF_age_26 ///
		 culturalrev $controls1 $controls2 i.FE_t i.FE_prov, cluster(prov) ltolerance(1e-1) iter(40)
	
	********************************
	* (2.2) Build the Life Table
	********************************
	keep if e(sample)
	keep if LLF==0
	
	* Because we use a nonlinear model, we predict the counterfactual and the counterfactual+LLF effect using a simulation approach
	* We will predict life table elements for each age for each bootstrap rep
	* We will store them in a few matrices for use later, each with a column for each age and a row for each bootstrap rep
			
	* First generate a simulated marginal risk of a birth without LLF
	* Replace LLF = 0 
	replace LLF = 0

	* Replace age*LLF interactions with zero and predict reference group
		forval i = 1/26 { 			
			replace LLF_age_`i' = 0
			} 
						
		cap drop xb
		predict xb, xb
			
		cap drop qx
		predict qx
			
		forval age = 15/40 { 
				
			sum xb if age == `age' 
			local xb = r(mean)
					
			sum qx if age == `age' 
			local qx = r(mean)
					
			if `age' == 15 {
				mat Xb_NoLLF = `xb'
				mat Qx_NoLLF = `qx' 
				} 
			else if `age' > 15 { 
				mat temp1 = `xb'
				mat temp2 = `qx'
				mat Xb_NoLLF = Xb_NoLLF, `xb'
				mat Qx_NoLLF = Qx_NoLLF, `qx' 
				}
			}

	* Now generate a simulated marginal risk of a birth with LLF
	* Replace LLF = 1 
	replace LLF = 1

	* Replace age*LLF interactions with zero and predict LLF
		forval i = 1/26 { 			
			replace LLF_age_`i' = 1 if age_`i' == 1
			} 
					
		cap drop xb
		predict xb, xb
			
		cap drop qx
		predict qx
			
		forval age = 15/40 { 
			sum xb if age == `age' 
			local xb = r(mean)
					
			sum qx if age == `age' 
			local qx = r(mean)
					
			if `age' == 15 {
				mat Xb_LLF = `xb'
				mat Qx_LLF = `qx' 
				} 
			else if `age' > 15 { 
				mat temp1 = `xb'
				mat temp2 = `qx'
				mat Xb_LLF = Xb_LLF, `xb'
				mat Qx_LLF = Qx_LLF, `qx' 
				}
			}
					
		* Using the Qx table, we compute Lx and Dx for each age with and without LLF
		* Lx table initialized with a value of 1 for those age 15
		forval age = 15/40 { 
			if `age' == 15 {
				mat Lx_NoLLF = 1
				mat Lx_LLF = 1
				mat Dx_NoLLF = Qx_NoLLF[1,1]*Lx_NoLLF[1,1]
				mat Dx_LLF = Qx_LLF[1,1]*Lx_LLF[1,1]
			}
			if `age' >15 { 
				local i = `age' - 15
				local j = `i' + 1
				mat temp1 = Lx_NoLLF[1, `i']-Dx_NoLLF[1, `i']
				mat temp2 = Lx_LLF[1, `i']-Dx_LLF[1, `i']
				mat Lx_NoLLF = Lx_NoLLF, temp1
				mat Lx_LLF = Lx_LLF, temp2
				
				mat temp1 = Qx_NoLLF[1,`j']*Lx_NoLLF[1,`j']
				mat temp2 = Qx_LLF[1,`j']*Lx_LLF[1,`j']				
				mat Dx_NoLLF = Dx_NoLLF, temp1
				mat Dx_LLF = Dx_LLF, temp2
			} 
		}
		
		* Marginal effect of LLF on Qx
		mat Marginal = Qx_LLF - Qx_NoLLF		
			
		foreach LTelement in Xb_NoLLF Xb_LLF Qx_NoLLF Qx_LLF Lx_NoLLF Lx_LLF Dx_NoLLF Dx_LLF Marginal { 
			* Add a col to each matrix containing life table elements to index the bootstrap rep (here it's missing because this is the main result)
			mat `LTelement' = . , `LTelement'
			
			* Label the columns  
			mat colnames `LTelement' = "bs_rep" "Age15" "Age16" "Age17" "Age18" "Age19" "Age20" "Age21" ///
									"Age22" "Age23" "Age24" "Age25" "Age26" "Age27" "Age28" "Age29" ///
									"Age30" "Age31" "Age32" "Age33" "Age34" "Age35" "Age36" "Age37" ///
									"Age38" "Age39" "Age40"
			}
	
	********************************
	* (2.3) Plot the CDF
	********************************
	* reshape the Lx column 
	mat tempLx_NoLLF = Lx_NoLLF[1, 2...]
		mat tempLx_NoLLF = tempLx_NoLLF'
	mat tempLx_LLF = Lx_LLF[1, 2...]
		mat tempLx_LLF = tempLx_LLF'	
	
	mat Birth1_LifeTable = tempLx_NoLLF, tempLx_LLF
	clear 
	svmat Birth1_LifeTable 
		rename Birth1_LifeTable1 Lx_NoLLF
		rename Birth1_LifeTable2 Lx_LLF
		
		gen Age = 14+_n
		gen Birth1_NoLLF = 1 - Lx_NoLLF
		gen Birth1_LLF = 1 - Lx_LLF

	twoway (connected Birth1_NoLLF Age, lpattern(solid) msize(zero)) ///
			(connected Birth1_LLF Age, lpattern(dash) msize(zero)) ///
			, legend(pos(6) order(1 "Before LLF" 2 "After LLF") rows(1)) ///
			 title("") xtitle("Age") ytitle("Share of Population" ) xlab(16(2)40) xline(23, lcolor(gs9)) ///
			text(.2 23 "Marriage Age" "Under LLF", size(small) box fcolor(white) bcolor(white))  ylab(0(.1)1, format(%9.2f))
			
	graph export "$d_fig\Fig_A14b_AgeFirstBirth.jpg", replace  height(1200) width(1800)	
	
	********************************************************
	* (2.4) Compute the change in mean age and median age
	********************************************************
	local mean_NoLLF = 0
	local dx_cum_NoLLF = 0 
	local flag_NoLLF = 1

	forval age = 15/38 { 
		local j = `age' - 1
		local int = `age'*Dx_NoLLF[1, "Age`age'"]
		local mean_NoLLF = `mean_NoLLF' + `int' 
		local dx = Dx_NoLLF[1, "Age`age'"]
		local dx_cum_NoLLF = `dx_cum_NoLLF' + `dx' 
		
		local y = Lx_NoLLF[1, "Age`age'"]
		if `y' < .5 & `flag_NoLLF' == 1 { 
			local flag_NoLLF = 2 
			local y_1 = Lx_NoLLF[1, "Age`j'"]
			local x = `age'
			local beta = `y' - `y_1'
			local alpha = `y'-(`beta'*`x')
			local median_NoLLF = (.5-`alpha')/`beta'
			}
	}		
	
	local mean_LLF = 0
	local dx_cum_LLF = 0 
	local flag_LLF = 1
	
	forval age = 15/38 { 
		local j = `age' - 1
		local int = `age'*Dx_LLF[1, "Age`age'"]
		local mean_LLF = `mean_LLF' + `int' 
		local dx = Dx_LLF[1, "Age`age'"]
		local dx_cum_LLF = `dx_cum_LLF' + `dx' 
		
		local y = Lx_LLF[1, "Age`age'"]
		if `y' < .5 & `flag_LLF' == 1 { 
			local flag_LLF = 2 
			local y_1 = Lx_LLF[1, "Age`j'"]
			local x = `age'
			local beta = `y' - `y_1'
			local alpha = `y'-(`beta'*`x')
			local median_LLF = (.5-`alpha')/`beta'
			}
	}	
	
	local mean_NoLLF = `mean_NoLLF'*`dx_cum_NoLLF'
	local mean_LLF = `mean_LLF'*`dx_cum_LLF'
	
	local dif_mean = `mean_LLF' - `mean_NoLLF'
	local dif_median = `median_LLF' - `median_NoLLF'
	
	mat effect = ., `mean_LLF', `mean_NoLLF', `dif_mean', `median_LLF', `median_NoLLF', `dif_median'
	mat colnames effect = "bs_rep" "MeanLLF" "MeanNoLLF" "DiffMean" "MedianLLF" "MedianNoLLF" "DiffMedian"

**********************************************************
* (3.0) Bootstrap standard errors for main results
**********************************************************
		
	set seed 365476247 
	
	forvalues b = 1/1000 { 

		di "***************************************"
		di "***************************************"
		di "***************************************"
		di "***************************************"
		di "********** BS Rep `b' *****************"
		di "***************************************"
		di "***************************************"
		di "***************************************"
		di "***************************************"

		* Start with the main dataset
		use `main', replace 
		
		* Do cluster-pairs bootstrap 
		* Draw a random sample of clusters from the main sample, 
		* rerun the regression, save the bootstrapped beta and wald
		
		bsample , cluster(prov) idcluster(newprov) 
		egen FE_prov_BS = group(newprov StackDataID)

		********************************
		* (3.1) Main Regression
		********************************		
		qui xi: logit mother age_1-age_8 age_10-age_26 LLF LLF_age_1-LLF_age_8 LLF_age_10-LLF_age_26 ///
			 culturalrev $controls2 $controls3 i.FE_prov_BS i.FE_t,  cluster(prov) iter(30)
	
		********************************
		* (3.2) Build Life Table
		********************************		
		keep if e(sample)
		keep if LLF==0
		
			* Because we use a nonlinear model, we predict the counterfactual and the 
			* counterfactual+LLF effect using a simulation approach
			* We will predict life table elements for each age for each bootstrap rep
			* We will store them in a few matrices for use later, each with a column for each age and a row for each bootstrap rep
			
			* First generate a simulated marginal risk of a birth without LLF
			* Replace LLF = 0 
			replace LLF = 0

			* Replace age*LLF interactions with zero and predict reference group
			forval i = 1/26 { 			
				replace LLF_age_`i' = 0
				} 
						
			cap drop xb
			predict xb, xb
			
			cap drop qx
			predict qx
			
				forval age = 15/40 { 
				
					qui sum xb if age == `age' 
					local xb = r(mean)
					
					qui sum qx if age == `age' 
					local qx = r(mean)
					
					if `age' == 15 {
						mat tXb_NoLLF = `xb'
						mat tQx_NoLLF = `qx' 
						} 
					else if `age' > 15 { 
						mat temp1 = `xb'
						mat temp2 = `qx'
						mat tXb_NoLLF = tXb_NoLLF, `xb'
						mat tQx_NoLLF = tQx_NoLLF, `qx' 
						}
					}

			* Now generate a simulated marginal risk of a birth with LLF
			* Replace LLF = 1 
			replace LLF = 1

			* Replace age*LLF interactions with zero and predict LLF
			forval i = 1/26 { 			
				replace LLF_age_`i' = 1 if age_`i' == 1
				} 
					
			cap drop xb
			predict xb, xb
			
			cap drop qx
			predict qx
			
				forval age = 15/40 { 
				
					qui sum xb if age == `age' 
					local xb = r(mean)
					
					qui sum qx if age == `age' 
					local qx = r(mean)
					
					if `age' == 15 {
						mat tXb_LLF = `xb'
						mat tQx_LLF = `qx' 
						} 
					else if `age' > 15 { 
						mat temp1 = `xb'
						mat temp2 = `qx'
						mat tXb_LLF = tXb_LLF, `xb'
						mat tQx_LLF = tQx_LLF, `qx' 
						}
					}
					
		* Using the Qx table, we compute Lx and Dx for each age with and without LLF
		* Lx table initialized with a value of 1 for those age 15
		forval age = 15/40 { 
			if `age' == 15 {
				mat tLx_NoLLF = 1
				mat tLx_LLF = 1
				mat tDx_NoLLF = tQx_NoLLF[1,1]*tLx_NoLLF[1,1]
				mat tDx_LLF = tQx_LLF[1,1]*tLx_LLF[1,1]
			}
			if `age' >15 { 
				local i = `age' - 15
				local j = `i' + 1
				mat temp1 = tLx_NoLLF[1, `i']-tDx_NoLLF[1, `i']
				mat temp2 = tLx_LLF[1, `i']-tDx_LLF[1, `i']
				mat tLx_NoLLF = tLx_NoLLF, temp1
				mat tLx_LLF = tLx_LLF, temp2
				
				mat temp1 = tQx_NoLLF[1,`j']*tLx_NoLLF[1,`j']
				mat temp2 = tQx_LLF[1,`j']*tLx_LLF[1,`j']				
				mat tDx_NoLLF = tDx_NoLLF, temp1
				mat tDx_LLF = tDx_LLF, temp2
			} 
		}
		
		* Marginal effect of LLF on Qx
		mat tMarginal = `b', tQx_LLF - tQx_NoLLF		
		mat Marginal = Marginal \ tMarginal
			
		foreach LTelement in Xb_NoLLF Xb_LLF Qx_NoLLF Qx_LLF Lx_NoLLF Lx_LLF Dx_NoLLF Dx_LLF  { 
			* Add a col to each matrix containing life table elements to index the bootstrap rep
			mat t`LTelement' = `b', t`LTelement'
			mat colnames t`LTelement' = "bs_rep" "Age15" "Age16" "Age17" "Age18" "Age19" "Age20" "Age21" ///
									"Age22" "Age23" "Age24" "Age25" "Age26" "Age27" "Age28" "Age29" ///
									"Age30" "Age31" "Age32" "Age33" "Age34" "Age35" "Age36" "Age37" ///
									"Age38" "Age39" "Age40"

			mat `LTelement' = `LTelement' \ t`LTelement'
		}
		
		********************************************************
		* (3.4) Compute the change in mean age and median age
		********************************************************
		* Now calculate implied change in mean and median age of marriage 
		* We'll have a column for each effect and 1000 rows (one for each rep)

		local mean_NoLLF = 0
		local dx_cum_NoLLF = 0 
		local flag_NoLLF = 1

		forval age = 15/38 { 
			local j = `age' - 1
			local int = `age'*tDx_NoLLF[1, "Age`age'"]
			local mean_NoLLF = `mean_NoLLF' + `int' 
			local dx = tDx_NoLLF[1, "Age`age'"]
			local dx_cum_NoLLF = `dx_cum_NoLLF' + `dx' 
			
			local y = Lx_NoLLF[1, "Age`age'"]
			if `y' < .5 & `flag_NoLLF' == 1 { 
				local flag_NoLLF = 2 
				local y_1 = tLx_NoLLF[1, "Age`j'"]
				local x = `age'
				local beta = `y' - `y_1'
				local alpha = `y'-(`beta'*`x')
				local median_NoLLF = (.5-`alpha')/`beta'
				}
		}		
		
		local mean_LLF = 0
		local dx_cum_LLF = 0 
		local flag_LLF = 1
		
		forval age = 15/38 { 
			local j = `age' - 1
			local int = `age'*tDx_LLF[1, "Age`age'"]
			local mean_LLF = `mean_LLF' + `int' 
			local dx = tDx_LLF[1, "Age`age'"]
			local dx_cum_LLF = `dx_cum_LLF' + `dx' 
			
			local y = Lx_LLF[1, "Age`age'"]
			if `y' < .5 & `flag_LLF' == 1 { 
				local flag_LLF = 2 
				local y_1 = tLx_LLF[1, "Age`j'"]
				local x = `age'
				local beta = `y' - `y_1'
				local alpha = `y'-(`beta'*`x')
				local median_LLF = (.5-`alpha')/`beta'
				}
		}	
		
		local mean_NoLLF = `mean_NoLLF'*`dx_cum_NoLLF'
		local mean_LLF = `mean_LLF'*`dx_cum_LLF'
		
		local dif_mean = `mean_LLF' - `mean_NoLLF'
		local dif_median = `median_LLF' - `median_NoLLF'
		
		mat teffect = `b', `mean_LLF', `mean_NoLLF', `dif_mean', `median_LLF', `median_NoLLF', `dif_median'
		mat effect = effect \ teffect 
		}

		clear 
		svmat effect, names(col) 
		sum DiffMedian if bs_rep==. 
		local diffmedian = r(mean)

		_pctile DiffMedian,  percentiles(2.5 97.5)
		local cil = r(r1) 
		local ciu = r(r2) 
		di as error "Change in median age at first birth: `diffmedian' [`cil' `ciu']"
		
		* We will plot the difference between the conditional risk of marriage at
		* a given age under LLF compared to pre-LLF
		clear 
		svmat Marginal, names(col) 
			
		* Grab the 95% range of the bootstrapped results
			
			forval age = 15/40 { 
				_pctile Age`age' ,  percentiles(2.5 97.5)
				local cil = r(r1) 
				local ciu = r(r2) 
				
				qui sum Age`age' 
				local mean_effect = r(mean)
				
				if `age' == 15 { 
					mat marginals_wci = `age', `mean_effect', `cil', `ciu'
					}
				else if `age' > 15 { 
					mat temp = `age', `mean_effect', `cil', `ciu'
					mat marginals_wci = marginals_wci \ temp 
					} 
				} 
				mat colnames marginals_wci  = "Age" "mean" "cil" "ciu"  		
	
**********************************************************
* (4.0) Plot the results
**********************************************************
	
		clear 
		svmat marginals_wci, names(col)
					
		twoway 	(scatter mean Age, mcolor(black) msymbol(solid)) ///
				(rcap cil ciu Age, lcolor(black)) ///
				if Age<=30 & Age>=17, legend(off) xtitle(Age) xlab(17(1)30) yline(0) xline(23, lcolor(gs9)) ///
				ytitle("Percentage Point Change") ///
				text(.25 23 "Marriage Age" "Under LLF", color(gs6) box bcolor(white))  ///
				yscale(range(-.2 .4)) ylab(-.2(.05).4, format(%9.2f))
		graph export "$d_fig\Fig_A14a_AgeFirstBirth.jpg", replace 	

	*******************************************************
	* (5.0) Appendix Table A5
	********************************************************
		* Make the appendix table columns
		clear 
		svmat Qx_NoLLF, names(col)
			forval age = 15/40 { 
				sum Age`age' if bs_rep == . 
					local pt_est = r(mean)
				_pctile Age`age',  percentiles(2.5 97.5)
					local cil = r(r1) 
					local ciu = r(r2) 
								
				if `age' == 15 { 
					mat temp1 = `age', `pt_est', `cil', `ciu'
					}
				else if `age' > 15 { 
					mat temp2 =  `age', `pt_est', `cil', `ciu'
					mat temp1 = temp1 \ temp2 
					} 
				} 
				mat colnames temp1  = "Age" "QxNoLLF" "QxNoLLFcil" "QxNoLLFciu"  
			
		mat LT_birth1 = temp1 
		
		clear 
		svmat Qx_LLF, names(col)
			forval age = 15/40 { 
				sum Age`age' if bs_rep == . 
					local pt_est = r(mean)
				_pctile Age`age',  percentiles(2.5 97.5)
					local cil = r(r1) 
					local ciu = r(r2) 
								
				if `age' == 15 { 
					mat temp1 = `pt_est', `cil', `ciu'
					}
				else if `age' > 15 { 
					mat temp2 =  `pt_est', `cil', `ciu'
					mat temp1 = temp1 \ temp2 
					} 
				} 
				mat colnames temp1  = "QxLLF" "QxLLFcil" "QxLLFciu"  
		mat LT_birth1 = LT_birth1, temp1 

		clear 
		svmat Lx_NoLLF, names(col)
			forval age = 15/40 { 
				sum Age`age' if bs_rep == . 
					local pt_est = r(mean)
				_pctile Age`age',  percentiles(2.5 97.5)
					local cil = r(r1) 
					local ciu = r(r2) 
								
				if `age' == 15 { 
					mat temp1 = `pt_est', `cil', `ciu'
					}
				else if `age' > 15 { 
					mat temp2 =  `pt_est', `cil', `ciu'
					mat temp1 = temp1 \ temp2 
					} 
				} 
				mat colnames temp1  = "LxNoLLF" "LxNoLLFcil" "LxNoLLFciu"  
			
		mat LT_birth1 = LT_birth1, temp1 
		
		clear 
		svmat Lx_LLF, names(col)
			forval age = 15/40 { 
				sum Age`age' if bs_rep == . 
					local pt_est = r(mean)
				_pctile Age`age',  percentiles(2.5 97.5)
					local cil = r(r1) 
					local ciu = r(r2) 
								
				if `age' == 15 { 
					mat temp1 = `pt_est', `cil', `ciu'
					}
				else if `age' > 15 { 
					mat temp2 =  `pt_est', `cil', `ciu'
					mat temp1 = temp1 \ temp2 
					} 
				} 
				mat colnames temp1  = "LxLLF" "LxLLFcil" "LxLLFciu"  
		mat LT_birth1 = LT_birth1, temp1 

		clear 
		svmat Dx_NoLLF, names(col)
			forval age = 15/40 { 
				sum Age`age' if bs_rep == . 
					local pt_est = r(mean)
				_pctile Age`age',  percentiles(2.5 97.5)
					local cil = r(r1) 
					local ciu = r(r2) 
								
				if `age' == 15 { 
					mat temp1 = `pt_est', `cil', `ciu'
					}
				else if `age' > 15 { 
					mat temp2 =  `pt_est', `cil', `ciu'
					mat temp1 = temp1 \ temp2 
					} 
				} 
				mat colnames temp1  = "DxNoLLF" "DxNoLLFcil" "DxNoLLFciu"  
			
		mat LT_birth1 = LT_birth1, temp1 
		
		clear 
		svmat Dx_LLF, names(col)
			forval age = 15/40 { 
				sum Age`age' if bs_rep == . 
					local pt_est = r(mean)
				_pctile Age`age',  percentiles(2.5 97.5)
					local cil = r(r1) 
					local ciu = r(r2) 
								
				if `age' == 15 { 
					mat temp1 = `pt_est', `cil', `ciu'
					}
				else if `age' > 15 { 
					mat temp2 =  `pt_est', `cil', `ciu'
					mat temp1 = temp1 \ temp2 
					} 
				} 
				mat colnames temp1  = "DxLLF" "DxLLFcil" "DxLLFciu"  
		mat LT_birth1 = LT_birth1, temp1 	
		
	* Export to excel 
	putexcel set  `"$d_tab\Table_A5.xlsx"', modify		
		local mrow = 1
		local trow1 = 4
		local trow2 = 5
		forval age = 15/40 {		
			local main = LT_birth1[`mrow', 2]
				local main = round(`main', .001)
				scalar main = string(`main', "%05.3f") 
				putexcel I`trow1' = "`=main'", hcenter
			local cil = LT_birth1[`mrow', 3]
				local cil = round(`cil', .001)
				scalar cil = string(`cil', "%05.3f") 
			local ciu = LT_birth1[`mrow', 4]
				local ciu = round(`ciu', .001)
				scalar ciu = string(`ciu', "%05.3f") 
				putexcel I`trow2' = "[`=cil' - `=ciu']", hcenter
				
			local main = LT_birth1[`mrow', 5]
				local main = round(`main', .001)
				scalar main = string(`main', "%05.3f") 
				putexcel J`trow1' = "`=main'", hcenter
			local cil = LT_birth1[`mrow', 6]
				local cil = round(`cil', .001)
				scalar cil = string(`cil', "%05.3f") 
			local ciu = LT_birth1[`mrow', 7]
				local ciu = round(`ciu', .001)
				scalar ciu = string(`ciu', "%05.3f") 
				putexcel J`trow2' = "[`=cil' - `=ciu']", hcenter
				
			local main = LT_birth1[`mrow', 8]
				local main = round(`main', .001)
				scalar main = string(`main', "%05.3f") 
				putexcel K`trow1' = "`=main'", hcenter
			local cil = LT_birth1[`mrow', 9]
				local cil = round(`cil', .001)
				scalar cil = string(`cil', "%05.3f") 
			local ciu = LT_birth1[`mrow', 10]
				local ciu = round(`ciu', .001)
				scalar ciu = string(`ciu', "%05.3f") 
				putexcel K`trow2' = "[`=cil' - `=ciu']", hcenter	
				
			local main = LT_birth1[`mrow', 11]
				local main = round(`main', .001)
				scalar main = string(`main', "%05.3f") 
				putexcel L`trow1' = "`=main'", hcenter
			local cil = LT_birth1[`mrow', 12]
				local cil = round(`cil', .001)
				scalar cil = string(`cil', "%05.3f") 
			local ciu = LT_birth1[`mrow', 13]
				local ciu = round(`ciu', .001)
				scalar ciu = string(`ciu', "%05.3f") 
				putexcel L`trow2' = "[`=cil' - `=ciu']"	, hcenter
				
			local main = LT_birth1[`mrow', 14]
				local main = round(`main', .001)
				scalar main = string(`main', "%05.3f") 
				putexcel M`trow1' = "`=main'", hcenter
			local cil = LT_birth1[`mrow', 15]
				local cil = round(`cil', .001)
				scalar cil = string(`cil', "%05.3f") 
			local ciu = LT_birth1[`mrow', 16]
				local ciu = round(`ciu', .001)
				scalar ciu = string(`ciu', "%05.3f") 
				putexcel M`trow2' = "[`=cil' - `=ciu']"	, hcenter
				
			local main = LT_birth1[`mrow', 17]
				local main = round(`main', .001)
				scalar main = string(`main', "%05.3f") 
				putexcel N`trow1' = "`=main'", hcenter
			local cil = LT_birth1[`mrow', 18]
				local cil = round(`cil', .001)
				scalar cil = string(`cil', "%05.3f") 
			local ciu = LT_birth1[`mrow', 19]
				local ciu = round(`ciu', .001)
				scalar ciu = string(`ciu', "%05.3f") 
				putexcel N`trow2' = "[`=cil' - `=ciu']", hcenter
				
			local mrow = `mrow'+1
			local trow1 = `trow1'+2
			local trow2 = `trow2'+2
		}
		
		putexcel save 